package com.im.partchat.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import com.im.partchat.R;

import java.util.ArrayList;
import java.util.List;


public class LetterListViewV2 extends View {

    OnTouchingLetterChangedListener onTouchingLetterChangedListener;
//    String[] b = {"#", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"
//            , "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};

    private List<String> alphas = new ArrayList<>();

    int choose = -1;
    Paint paint = new Paint();
    boolean showBkg = false;
    float startPos;
    float endPos;

    private TextView pop;

    private boolean firstInit = true;

    private int miniSingleHeight = 0;

    public void setPop(TextView pop) {
        this.pop = pop;
    }

    public LetterListViewV2(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public LetterListViewV2(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public LetterListViewV2(Context context) {
        super(context);
    }

    /**
     * 从外部传入需要显示的字母
     * @param alphas 要显示的字母
     */
    public void setAlphas(List<String> alphas){
        this.alphas = alphas;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (null == alphas || alphas.size() == 0){
            return;
        }
        if (showBkg) {
            canvas.drawColor(Color.parseColor("#00000000"));
        }

        int height = getHeight();
        int width = getWidth();
        if (firstInit) {
            firstInit = false;
        }

        int singleHeight = (height - getContext().getResources().getDimensionPixelOffset(R.dimen.padding_72)) / 26;

        if (alphas.size() < 20 && alphas.size() >= 10){
            singleHeight *= 1.4f;
        } else if (alphas.size() < 10 ){
            singleHeight *= 2;
        }

        int bcount = alphas.size();
        for (int i = 0; i < bcount; i++) {
            paint.setColor(getResources().getColor(R.color.text_black_color));
            paint.setTextSize(getResources().getDimensionPixelSize(R.dimen.size4));
            paint.setAntiAlias(true);
            float xPos = width / 2 - paint.measureText(alphas.get(i)) / 2;
            float yPos = height / 2 - (bcount / 2 - i) * singleHeight + singleHeight / 2;
            if (i == 0){
                startPos = yPos - singleHeight;
            } else if (i == bcount-1){
                endPos = yPos;
            }

            if (yPos >= height) {
                break;
            }
            canvas.drawText(alphas.get(i), xPos, yPos, paint);
            paint.reset();
        }

    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        final int action = event.getAction();
        final float y = event.getY();
        final int oldChoose = choose;
        final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;

        int c = -1;
        if (y >= startPos && y <= endPos){
            c = (int) ((y - startPos) / (endPos - startPos) * alphas.size());
        }

        switch (action) {
            case MotionEvent.ACTION_DOWN:
                showBkg = true;
                if (oldChoose != c && listener != null) {
                    if (c >= 0 && c < alphas.size()) {
                        listener.onTouchingLetterChanged(alphas.get(c));
                        if (pop != null) {
                            pop.setText(alphas.get(c));
                            pop.setVisibility(VISIBLE);
                        }
//                        setBackgroundColor(getResources().getColor(R.color.cor3));
                        choose = c;
                        invalidate();
                    }
                }

                break;
            case MotionEvent.ACTION_MOVE:
                if (oldChoose != c && listener != null) {
                    if (c >= 0 && c < alphas.size()) {
                        listener.onTouchingLetterChanged(alphas.get(c));
                        if (pop != null) {
                            pop.setText(alphas.get(c));
                            pop.setVisibility(VISIBLE);
                        }
//                        setBackgroundColor(getResources().getColor(R.color.cor3));
                        choose = c;
                        invalidate();
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                showBkg = false;
                if (pop != null) {
                    if (c >= 0 && c < alphas.size()) {
                        pop.setText(alphas.get(c));
                    }
                    pop.setVisibility(INVISIBLE);
                }
//                setBackgroundColor(Color.TRANSPARENT);
                choose = -1;
                invalidate();
                break;
        }
        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }

    public void setOnTouchingLetterChangedListener(
            OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
        this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
    }

    public interface OnTouchingLetterChangedListener {
        void onTouchingLetterChanged(String s);
    }

    public void showBkgVisible(boolean show) {
        this.showBkg = show;
    }

}